//
//  YYChangePwdViewController.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/22.
//  Copyright © 2017年 stoneobs. All rights reserved.
////**********************修改密码***************************

import UIKit

class YYChangePwdViewController: STTableViewController {

    
    var pwdOriginTextFiled:UITextField?
    

    var pwdTextFiled:UITextField?
    
    
    var pwdConfirmTextFiled:UITextField?
    
    var finshButton:STButton?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "修改密码";
        self.view.backgroundColor = .white;
        self.tableView.backgroundColor = .white;
        self.initSubviews();
        NotificationCenter.default.addObserver(self, selector: #selector(didReciveNotifacationAction(notify:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil);
        // Do any additional setup after loading the view.
    }
    
    func didReciveNotifacationAction(notify:NSNotification) -> Void {
        
        if (self.pwdOriginTextFiled?.text?.characters.count)! > 5  &&
            (self.pwdTextFiled?.text?.characters.count)! > 5 &&
            (self.pwdConfirmTextFiled?.text?.characters.count)! > 5{
            
            self.finshButton?.isUserInteractionEnabled = true;
            self.finshButton?.backgroundColor = UIColor.flatMint;
            
        }else{
            
            self.finshButton?.isUserInteractionEnabled = false;
            self.finshButton?.backgroundColor = NO_USER_INTERFACE_COLOR;
        }
    }
    deinit {
        NotificationCenter.default.removeObserver(self);
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        
        self.findHairlineImageViewUnder(self.navigationController?.navigationBar).isHidden = true;
        
        
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated);
        
        self.findHairlineImageViewUnder(self.navigationController?.navigationBar).isHidden = false;
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func initSubviews() -> Void {
        
        self.pwdOriginTextFiled = UITextField.init(frame: .init(x: 25, y: 10, width: SCREEN_WIDTH - 50 , height: 40));
        self.pwdOriginTextFiled?.placeholder = "请输入原密码";
        self.pwdOriginTextFiled?.keyboardType = .numberPad;
        self.pwdOriginTextFiled?.textAlignment = .left;
        self.pwdOriginTextFiled?.isSecureTextEntry = true;
        self.pwdOriginTextFiled?.clearButtonMode = .whileEditing;
        self.tableView.addSubview(self.pwdOriginTextFiled!);
        
        let accountLine:UIView = UIView.init(frame: CGRect.init(x: 0, y: (self.pwdOriginTextFiled?.bottom)!, width: SCREEN_WIDTH - 50, height: 1));
        accountLine.backgroundColor = LINE_COLOR;
        accountLine.centerX = SCREEN_WIDTH / 2;
        self.tableView.addSubview(accountLine);
        
        
        self.pwdTextFiled = UITextField.init(frame: .init(x: 25, y: (self.pwdOriginTextFiled?.bottom)! + 20, width: SCREEN_WIDTH - 50, height: 40));
        self.pwdTextFiled?.placeholder = "请输入新密码";
        self.pwdTextFiled?.keyboardType = .asciiCapable;
        self.pwdTextFiled?.centerX = SCREEN_WIDTH / 2;
        self.pwdTextFiled?.textAlignment = .left;
        self.pwdTextFiled?.isSecureTextEntry = true;
        self.pwdTextFiled?.clearButtonMode = .whileEditing;
        self.tableView.addSubview(self.pwdTextFiled!);
        
        let pwdLine:UIView = UIView.init(frame: CGRect.init(x: 0, y: (self.pwdTextFiled?.bottom)!, width: accountLine.width, height: 1));
        pwdLine.backgroundColor = LINE_COLOR;
        pwdLine.centerX = SCREEN_WIDTH / 2;
        self.tableView.addSubview(pwdLine);
        
        
        self.pwdConfirmTextFiled = UITextField.init(frame: .init(x: 25, y: (self.pwdTextFiled?.bottom)! + 20, width: SCREEN_WIDTH - 50, height: 40));
        self.pwdConfirmTextFiled?.placeholder = "请再次输入新密码";
        self.pwdConfirmTextFiled?.keyboardType = .asciiCapable;
        self.pwdConfirmTextFiled?.centerX = SCREEN_WIDTH / 2;
        self.pwdConfirmTextFiled?.textAlignment = .left;
        self.pwdConfirmTextFiled?.isSecureTextEntry = true;
        self.pwdConfirmTextFiled?.isSecureTextEntry = true;
        self.pwdConfirmTextFiled?.clearButtonMode = .whileEditing;
        self.tableView.addSubview(self.pwdConfirmTextFiled!);
        
        let pwdConfrimLine:UIView = UIView.init(frame: CGRect.init(x: 0, y: (self.pwdConfirmTextFiled?.bottom)!, width: accountLine.width, height: 1));
        pwdConfrimLine.backgroundColor = LINE_COLOR;
        pwdConfrimLine.centerX = SCREEN_WIDTH / 2;
        self.tableView.addSubview(pwdConfrimLine);
        

        
        weak var weakSelf = self;
        
        self.finshButton = STButton.init(frame: .init(x: 45, y: (self.pwdConfirmTextFiled?.bottom)! + 40, width: SCREEN_WIDTH - 90, height: 40),
                                         title: "提交",
                                         titleColor: .white,
                                         titleFont: 15,
                                         cornerRadius: 3,
                                         backgroundColor: NO_USER_INTERFACE_COLOR,
                                         backgroundImage: nil,
                                         image: nil);
        self.finshButton?.isUserInteractionEnabled = false;
        self.finshButton?.centerX = SCREEN_WIDTH / 2;
        self.finshButton?.contentHorizontalAlignment = .center;
        self.finshButton?.clicAction = { but in
            
            weakSelf?.loadChangePwdRequest();
        }
        self.tableView.addSubview(self.finshButton!);
        
    }
    
        // MARK:Network Request

    
    func loadChangePwdRequest() -> Void {
        
        if self.pwdConfirmTextFiled?.text != self.pwdTextFiled?.text {
            SVProgressHUD.showError(withStatus: "两次输入密码不一致，请重新输入")
            return;
        }
        SVProgressHUD.show(withStatus: "加载中");
        self.finshButton?.isUserInteractionEnabled = false;
        BmobUser.current().updateCurrentUserPassword(withOldPassword: self.pwdOriginTextFiled?.text, newPassword: self.pwdTextFiled?.text) { (susccess, error) in
            
            SVProgressHUD.dismiss()
            self.finshButton?.isUserInteractionEnabled = true;
            if susccess == true {
            
                SVProgressHUD.showSuccess(withStatus: "修改密码成功，为了爱卿的账号安全，请重新登陆");
                BmobUser.logout();
                UserDefaults.standard.removeObject(forKey: YYLOGIN_TOKEN);
                UserDefaults.standard.synchronize();
                UIApplication.shared.keyWindow?.rootViewController = STNavigationController.init(rootViewController: YYLoginViewController());
                STLayoutViewController.defaultLayout().dismissCurrentController(false);
            }else{
            
                SVProgressHUD.showError(withStatus: YYUtils.bmobErrorDes(error: error!));
            }
        }
        
        
    }



}
